package com.black.web.controller.exercise;

import com.black.app.domain.exercise.AppExerciseType;
import com.black.app.domain.req.exercise.type.ExerciseTypeAddReq;
import com.black.app.domain.req.exercise.type.ExerciseTypeEditReq;
import com.black.app.domain.resp.exercise.AppExecrciseTypeInfoResp;
import com.black.app.domain.resp.exercise.AppExecrciseTypeResp;
import com.black.app.domain.resp.exercise.AppExerciseTreeResp;
import com.black.app.service.exercise.AppExerciseTypeService;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台-锻炼类型Api")
@RestController
@RequestMapping(value = "/admin/exercise/type")
public class AdminExerciseTypeController extends BaseController {


    /**
     * 锻炼类型业务
     */
    @Autowired
   private AppExerciseTypeService appExerciseTypeService;



    @ApiOperation(value = "查询表格树")
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('exercise:type:list')")
    public R<List<AppExerciseTreeResp>> backSearchTableTree(){
       List<AppExerciseTreeResp> list= this.appExerciseTypeService.backSearchTableTree();
        return R.ok(list);
    }


    @ApiOperation(value = "分页查询锻炼系列")
    @PreAuthorize("@ss.hasPermi('exercise:type:query')")
    @GetMapping(value = "/{id}")
    public R<AppExecrciseTypeResp> findExecriseTypeById(@PathVariable(value = "id") Long id){
        AppExerciseType exerciseType = this.appExerciseTypeService.getById(id);
        AppExecrciseTypeResp resp=new AppExecrciseTypeResp();
        BeanUtils.copyProperties(exerciseType,resp);
        if(resp.getComponentType()==null){
            resp.setComponentType(3);
        }
        return R.ok(resp);
    }

    @ApiOperation(value = "新增锻炼类型")
    @PreAuthorize("@ss.hasPermi('exercise:type:add')")
    @PostMapping
    public R<Boolean> addExerciseType(@RequestBody @Valid ExerciseTypeAddReq req){
        boolean result=this.appExerciseTypeService.add(req);
        return super.isSuccess(result);
    }

    @ApiOperation(value = "修改锻炼类型")
    @PreAuthorize("@ss.hasPermi('exercise:type:edit')")
    @PutMapping
    public R<Boolean> updateExerciseType(@RequestBody @Valid ExerciseTypeEditReq req){
        boolean result=this.appExerciseTypeService.edit(req);
        return super.isSuccess(result);
    }


    @ApiOperation(value = "根据主键查询详情")
    @PreAuthorize("@ss.hasPermi('exercise:type:query')")
    @GetMapping("/search/{id}")
    public R<AppExecrciseTypeInfoResp> searchById(@PathVariable(value = "id") Long id){
        AppExecrciseTypeInfoResp resp= this.appExerciseTypeService.searchById(id);
        return R.ok(resp);
    }



}
